package ace.module.captcha.core.impl.manager.impl;

import ace.cmp.util.AceCodecUtils;
import ace.module.captcha.api.enums.CaptchaDataImageTypeEnum;
import ace.module.captcha.core.impl.manager.CaptchaDataImageManager;
import java.io.ByteArrayOutputStream;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * @author caspar
 * @date 2023/10/7 14:10
 */
@AllArgsConstructor
@Component
public class CaptchaDataImageManagerImpl implements CaptchaDataImageManager {
  @Override
  public String toBase64Data(ByteArrayOutputStream os) {
    return this.toBase64Data(CaptchaDataImageTypeEnum.JPEG, os);
  }

  @Override
  public String toBase64Data(CaptchaDataImageTypeEnum imageTypeEnum, ByteArrayOutputStream os) {
    String base64Data =
        imageTypeEnum.getFormatName() + AceCodecUtils.encodeBase64(os.toByteArray());
    return base64Data;
  }
}
